package com.ajx.platform.buildingmronitor.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    public static final DateTimeFormatter yyyyMMddFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter yyyyMMddHHMMSSFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter yyyyMMddHHMMssSSSFmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    public static final DateTimeFormatter yyyyMMddHHMMssFmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * @Description: LocalDate转String
     * @Date: 13:30 2019/4/20
     * @return: dateStr yyyy-MM-dd
     **/
    public static String localDateToString(LocalDate date) {
        String dateStr = yyyyMMddFmt.format(date);
        return dateStr;
    }

    /**
     * @Description: LocalDate转String
     * @Date: 13:30 2019/4/20
     * @return: dateStr yyyy-MM-dd HH:mm:ss
     **/
    public static String localDateTimeToString(LocalDateTime time) {
        String dateStr = yyyyMMddHHMMSSFmt.format(time);
        return dateStr;
    }

    /**
     * LocalDate转Date
     * @param localDate
     * @example Wed May 29 00:00:00 CST 2019
     */
    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * LocalDateTime转换为Date
     * @param localDateTime
     * @example Wed May 29 10:32:53 CST 2019
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        if (null == localDateTime) {
            return null;
        }
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取当前时间字符串(毫秒)
     * @example 20190821153527453
     */
    public static String getCurrentTimeMillisStr() {
        return yyyyMMddHHMMssSSSFmt.format(LocalDateTime.now());
    }

    /**
     * 获取当前时间字符串
     * @example 20190821153527
     */
    public static String getCurrentTimeStr() {
        return yyyyMMddHHMMssFmt.format(LocalDateTime.now());
    }
}
